<?php

class Admin_FeatureDetailController extends My_Controller_Action {

    private $_model = '';
    private $_moduleName = '';
    private $_controllerName = '';
    private $_actionName = '';
    private $_actionMain = '';
    private $_params = array();
    private $_userId = '';
    private $_userName = '';

    public function init() {
        My_Auth_User::authenticate();

        $template_path = TEMPLATE_PATH . "/admin";
        $this->loadTemplate($template_path, 'template.ini', 'template');

        $this->_model = new Admin_Model_FeatureDetail();
        $this->_moduleName = $this->_request->getModuleName();
        $this->_controllerName = $this->_request->getControllerName();
        $this->_actionName = $this->_request->getActionName();
        $this->_params = $this->_request->getParams();
        $this->_actionMain = '/' . $this->_moduleName . '/' . $this->_controllerName . '/index';

        $this->view->params = $this->_params;
        $this->view->controllerName = $this->_controllerName;

        $auth = Zend_Auth::getInstance();
        $infoUser = $auth->getIdentity();
        $this->_userId = $infoUser['userId'];
        $this->_userName = $infoUser['username'];
    }

    public function indexAction() {
        $this->_redirect("/{$this->_moduleName}/{$this->_controllerName}/show-all");
    }

    public function showAllAction() {
        $featureDetail = $this->_model->getFeatureDetail();

        $this->view->featureDetail = $featureDetail;
    }

    public function addAction() {
    	$template_path = TEMPLATE_PATH . "/admin";
    	$this->loadTemplate($template_path, 'template.ini', 'ajax');

    	$formOption['featureId'] = $this->_params['featureId'];
    	
    	$categoryPath = $this->_request->getParam('categoryPath', null);
    	//$categoryPath = $this->_params['categoryPath'];
    	
        $categoryModel = new Admin_Model_Category();
        $formOption['category'] = $categoryModel->getFetchPairs(array('categoryPath'=>$categoryPath), array('task' => 'by-ids'));
        
        $form = new Admin_Form_FeatureDetail($formOption);
        
        if ($this->_request->isPost()) {
        	$data = $this->_request->getPost();
            $valid = new Admin_Form_Validators_FeatureDetailValid($data);

            if ($form->isValid($data) && $valid->isValid()) {
                $data = $form->getValues();
                $data['FeatureId'] = $this->_params['FeatureId'];
                $data['createdBy'] = $this->_userId;
                $data['createdByUserName'] = $this->_userName;
                $featureDetailId = $this->_model->addItem($data);

                if ($featureDetailId) {
                    if (isset($this->_params['btnSave'])) {
                        $this->view->popupMessage = $this->_helper->popupMessage(MESS_ADD, $this->_actionMain);
                    } elseif (isset($this->_params['btnSaveAdd'])) {
                        $this->_redirect("/{$this->_moduleName}/{$this->_controllerName}/{$this->_actionName}");
                    }
                }
            } else {
                $this->view->error = $valid->messages;
            }
        }

        $this->view->form = $form;
    }

    public function editAction() {
    	$template_path = TEMPLATE_PATH . "/admin";
    	$this->loadTemplate($template_path, 'template.ini', 'ajax');
    	 
        $featureDetailId = $this->_params['id'];

        if ($this->_request->isPost()) {
            $data = $this->_request->getPost();
        } else {
            $data = $this->_model->getFeatureDetailOne($featureDetailId, array('task' => 'feature'));
        }
        $this->view->data = $data;
        
        $formOption['featureId'] = $data['featureId'];
    	
    	$categoryPath = $data['categoryPath'];
    	//$categoryPath = $this->_params['categoryPath'];
    	
        $categoryModel = new Admin_Model_Category();
        $formOption['category'] = $categoryModel->getFetchPairs(array('categoryPath'=>$categoryPath), array('task' => 'by-ids'));
        
        $form = new Admin_Form_FeatureDetail($formOption);

        if ($this->_request->isPost()) {
            $valid = new Admin_Form_Validators_FeatureDetailValid($data);

            if ($form->isValid($data) && $valid->isValid()) {
                $data = $form->getValues();

                $data['FeatureId'] = $this->_params['FeatureId'];
                $data['modified'] = date(DATETIME_FORMAT_DB, time());
                $data['modifiedBy'] = $this->_userId;
                $result = $this->_model->updateItem($FeatureId, $data);
                   
                if ($result) {
                    if (isset($this->_params['btnSave'])) {
                        $this->view->popupMessage = $this->_helper->popupMessage(MESS_UPDATE, $this->_actionMain);
                    } elseif (isset($this->_params['btnSaveAdd'])) {
                        $this->_redirect("/{$this->_moduleName}/{$this->_controllerName}/add");
                    }
                }
            } else {
                $this->view->error = $valid->messages;
            }
        }
                        
        $form->populate($data);
        $this->view->form = $form;                
        $this->view->id = $featureDetailId;                
    }

    /**
     * add multi data
     */
    function ajaxAddAction() {
    	$template_path = TEMPLATE_PATH . "/admin";
    	$this->loadTemplate($template_path, 'template.ini', 'ajax');
    	//$this->_helper->viewRenderer->setNoRender();
    
    	$data = $this->_params['data'];    	
    	$data['createdBy'] = $this->_userId;
    	
    	$featureDetailId = $this->_model->addItem($data);
    	$result = $data;
    	$result['id'] = $featureDetailId;
    	
    	$featureDetailModel = new Admin_Model_FeatureDetail();
    	$featureDetail = $featureDetailModel->getFeatureDetail($data['featureId'], array('task' => 'admin-fetureCategory'));
    	
    	$arrFeatureDetail = null;
    	foreach($featureDetail as $key => $value){
    		$arrFeatureDetail[$value['categoryId']]['categoryName'] = $value['categoryName'];
    		$arrFeatureDetail[$value['categoryId']][] = $value;
    	}
    	
    	$this->view->arrFeatureDetail = $arrFeatureDetail; 
    }
    
    /**
     * edit multi data
     */
    function ajaxEditAction() {
        $template_path = TEMPLATE_PATH . "/admin";
        $this->loadTemplate($template_path, 'template.ini', 'ajax');
        //$this->_helper->viewRenderer->setNoRender();

        $data = $this->_params['data'];    	
    	$data['modified'] = date(DATETIME_FORMAT_DB, time());
        $data['modifiedBy'] = $this->_userId;
        $featureDetailId = $data['id'];
        unset($data['id']); 
    	
        $result = $this->_model->updateItem($featureDetailId, $data);
           
        $featureDetailModel = new Admin_Model_FeatureDetail();
    	$featureDetail = $featureDetailModel->getFeatureDetail($data['featureId'], array('task' => 'admin-fetureCategory'));
    	
    	$arrFeatureDetail = null;
    	foreach($featureDetail as $key => $value){
    		$arrFeatureDetail[$value['categoryId']]['categoryName'] = $value['categoryName'];
    		$arrFeatureDetail[$value['categoryId']][] = $value;
    	}
    	
    	$this->view->arrFeatureDetail = $arrFeatureDetail; 
    }
    
    /**
     * edit quick multi data
     */
    function ajaxEditQuickAction() {
    	$template_path = TEMPLATE_PATH . "/admin";
    	$this->loadTemplate($template_path, 'template.ini', 'ajax');
    	//$this->_helper->viewRenderer->setNoRender();
    
    	$data = $this->_params['data'];
    	$data['modified'] = date(DATETIME_FORMAT_DB, time());
    	$data['modifiedBy'] = $this->_userId;
    	$featureDetailId = $data['id'];
    	unset($data['id']);
    	 
    	$result = $this->_model->updateItem($featureDetailId, $data);
    	$featureDetail = $this->_model->getFeatureDetailOne($featureDetailId);
    	$this->view->featureDetail = $featureDetail;
    }

    function ajaxEditFeatureDetailAction() {
    	$template_path = TEMPLATE_PATH . "/admin";
    	$this->loadTemplate($template_path, 'template.ini', 'ajax');
    	$this->_helper->viewRenderer->setNoRender();
    
    	$id = $this->_params['id'];
    	$field = $this->_params['field'];
    	$value = $this->_params['value'];
    
    	$data[$field] = $value;
    
    	$result = array(
    			'result' => $this->_model->updateItem($id, $data),
    			'value' => $value,
    	);
    	echo json_encode($result);
    	die;
    }
    
    
    function ajaxRemoveAction() {
        $template_path = TEMPLATE_PATH . "/admin";
        $this->loadTemplate($template_path, 'template.ini', 'ajax');
        $this->_helper->viewRenderer->setNoRender();
               
        $actionName = $this->_params['actionName'];
        
        if($actionName == 'REMOVE_ROW'){
        	$featureDetailId = $this->_params['data'];
        	$result = $this->_model->removeItem($featureDetailId);
        }elseif($actionName == 'REMOVE_SELECT'){
        	$data = $this->_params['data'];
        	$result = $this->_model->removeItem($data, array('task'=>'remove-select'));
        }elseif($actionName == 'REMOVE_ALL'){        	
        	$result = $this->_model->removeItem('', array('task'=>'remove-all'));
        }
       	
        $results = array('result' => $result);
        
        if($actionName == 'REMOVE_ROW'){
        	$results['value'] = $featureDetailId;
        }elseif ($actionName == 'REMOVE_SELECT'){
        	$results['value'] = $data;
        }
        
        echo json_encode($results);
        die;        
    }
    
    function ajaxSearchAction() {
    	$template_path = TEMPLATE_PATH . "/admin";
    	$this->loadTemplate($template_path, 'template.ini', 'ajax');
    	
    	$data = $this->_params['data'];
    	
    	$featureDetail = $this->_model->getFeatureDetail($data, array('task'=>'admin-search'));

        $this->view->featureDetail = $featureDetail;
    }
    
    function ajaxGetFeatureDetailAction(){
    	$template_path = TEMPLATE_PATH . "/admin";
    	$this->loadTemplate($template_path, 'template.ini', 'ajax');
    	 
    	$data = $this->_params['data'];
    	 
    	$featureDetail = $this->_model->getFeatureDetail($data, array('task'=>'admin-category'));
    	
    	$tmpModel = new Admin_Model_Tmp();
    	$tmps = $tmpModel->getTmp(array('task'=>'admin-user'), array('userId' => $this->_userId));
    	
    	$arrTmp = null;
    	foreach($tmps as $key => $value){
    		$arrTmp[$value['featureDetailId']][] = $value;    		
    	}
    	 
    	$arrFeatureDetail = null;
    	foreach($featureDetail as $key => $value){
    		$arrFeatureDetail[$value['featureId']]['FeatureName'] = $value['FeatureName'];
    		$arrFeatureDetail[$value['featureId']]['isDetailProduct'] = $value['isDetailProduct'];
    		$arrFeatureDetail[$value['featureId']][] = $value;
    	}

    	$this->view->featureDetail = $arrFeatureDetail;
    	$this->view->tmp = $arrTmp;
    }

}